﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WhatSNew.AppCode;
using System.Windows;
namespace WhatSNew
{
    public partial class submission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Commit.Attributes.Add("onclick","return window.confirm('将要删除此记录，确定吗？');");
        }

        protected void Commit_Click(object sender, EventArgs e)
        {
            //Response.Write("<script>alert('删除成功!')</script>");
            if (!CheckBox1.Checked)
            {
                // Response.Write("<script>alert('请选择同意投稿须知！')");
                 Response.Write("<script>alert('请选择同意投稿须知！');window.location.href ='submission.aspx'</script>");
            }
            else
            {
                News m_news = new News();
                m_news.Content = InputNews.Text;
                m_news.Title = Text_Title.Text;
                UserInfo userInfo = (UserInfo)Session["user"];
                if (userInfo == null)
                {
                    m_news.UserBelong = -1;
                }
                else
                    m_news.UserBelong = userInfo.MInfo.ID;


                if (FileUpload1.HasFile)
                {
                    string typepic = FileUpload1.PostedFile.ContentType;
                    int imgsize = FileUpload1.PostedFile.ContentLength;
                    //对获取的格式进行判断
                    if (FileUpload1.PostedFile.ContentType.Substring(0, 5) == "image")
                    //if(true)
                    {
                        if (imgsize / 1024 < 600)//判断是否超级过600KB
                        {
                            try
                            {
                                string serverPath = Server.MapPath("upLoad");
                                if (!System.IO.Directory.Exists(serverPath))
                                    System.IO.Directory.CreateDirectory(serverPath);
                                string fileName = FileUpload1.FileName;
                                string newPath = serverPath + "\\" + fileName;
                                FileUpload1.SaveAs(newPath);
                                m_news.Image = "upLoad" + "\\" + fileName; ;
                                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('上传成功')", true);
                            }
                            catch
                            {
                                Response.Write("<script>alert('图片上传失败！!')</script>");
                                Response.Redirect("submission.aspx");
                            }
                        }
                        else
                        {
                            //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('图片大小请控制在600KB以下')", true);
                            Response.Write("<script>alert('图片大小请控制在600KB以下！!')</script>");
                            Response.Redirect("submission.aspx");
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert('图片格式不正确！')</script>");
                        Response.Redirect("submission.aspx");
                        // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('图片格式不正确')", true);
                    }
                }


                NewsInfoAccess m_newsInfoAccess = new NewsInfoAccess();
                m_newsInfoAccess.Insert(m_news);
                Response.Write("<script>alert('投稿成功！请耐心等待管理员审核！!');window.location.href ='submission.aspx'</script>");
                //News m_news = new News();
                //this.Attributes.Add("onclick", "javascript:return window.confirm('确认更改吗?')");
                //MessageBox.Show("hello", "world!", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                // Response.Write（"<script language=javascript>alert('投稿成功！请耐心等待管理员审核！');</script>")
                // Response.Write("投稿成功！请耐心等待管理员审核！");      
            }
                  
        }
    }
}